package com.wind.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.wind.dto.req.CounselorQueryConditionReqDto;
import com.wind.entity.AdeptDirection;
import com.wind.entity.Carousel;
import com.wind.entity.Counselor;
import com.wind.service.IAdeptDirectionService;
import com.wind.service.ICarouselService;
import com.wind.service.ICounselorService;
import com.wind.vo.ResultData;
import com.wind.vo.ResultDataGenerator;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * Created by Administrator on 2021/5/26.
 */
@Api(tags = "咨询师首页")
@RestController
@RequestMapping("/consultanthomepage")
public class ConsultantHomepageController {

    @Resource
    ICarouselService carouselService;
    @Resource
    ICounselorService counselorService;
    @Resource
    IAdeptDirectionService adeptDirectionService;
    @ApiOperation("轮播图列表")
    @GetMapping("/carouselList")
    public ResultData<List<Carousel>> carouselList(){
        Wrapper wrapper = new EntityWrapper<Carousel>();
        wrapper.eq("is_active", 1);
        wrapper.eq("location",2);
        wrapper.orderBy("level",false);
        wrapper.last("limit 3");
        List<Carousel> list = carouselService.selectList(wrapper);
        return ResultDataGenerator.genSuccessResult(list,"查询成功");
    }
    @ApiOperation("心理咨询列表")
    @GetMapping("/counselorList")
    public ResultData<List<Counselor>> counselorList(){
        List<Counselor> list = counselorlList(1);
        return ResultDataGenerator.genSuccessResult(list,"查询成功");
    }
    @ApiOperation("生涯规划列表")
    @GetMapping("/plannerList")
    public ResultData<List<Counselor>> plannerList(){
        List<Counselor> list = counselorlList(2);
        return ResultDataGenerator.genSuccessResult(list,"查询成功");
    }
    @ApiOperation("心理咨询更多")
    @GetMapping("/counselormore")
    public ResultData<List<Counselor>>  counselormore(){
        List<Counselor> list = counselorMore(1);
        return ResultDataGenerator.genSuccessResult(list,"查询成功");
    }
    @ApiOperation("规划师更多")
    @GetMapping("/plannermore")
    public ResultData<List<Counselor>>  plannermore(){
        List<Counselor> list = counselorMore(2);
        return ResultDataGenerator.genSuccessResult(list,"查询成功");
    }

    public List<Counselor>  counselorlList(Integer type){
        Wrapper wrapper = new EntityWrapper<Counselor>();
        wrapper.eq("is_active",1);
        wrapper.eq("type",type);
        wrapper.eq("recommend",1);
        wrapper.orderBy("advisory_count", false);
        wrapper.last("limit 3");
        List<Counselor> list = counselorService.selectList(wrapper);
        return list;
    }
    public List<Counselor> counselorMore(Integer type){
        Wrapper wrapper = new EntityWrapper<Counselor>();
        wrapper.eq("is_active",1);
        wrapper.eq("type",type);
        wrapper.orderBy("recommend",false);
        wrapper.orderBy("advisory_count",false);
        List<Counselor> list = counselorService.selectList(wrapper);
        return list;
    }
    @ApiOperation("搜索")
    @GetMapping("/search")
    public ResultData<Map<String,Object>> search(@RequestParam(value = "key", required = false, defaultValue = "") String key){
        Wrapper counselorWrapper = new EntityWrapper<Counselor>();
        counselorWrapper.eq("is_active", 1);
        if(Objects.nonNull(key) && !"".equals(key)){
            counselorWrapper.andNew().like("name", key).or().like("address", key).or().like("technical_post", key).or().like("adeptdirection", key);
        }
        List<Counselor> list = counselorService.selectList(counselorWrapper);
        //咨询师
        List<Counselor> counselorList = list.stream().filter(counselor -> counselor.getType() == 1).collect(Collectors.toList());
        //规划师
        List<Counselor> plannerList = list.stream().filter(counselor -> counselor.getType() == 2).collect(Collectors.toList());
        Map<String,Object> map = new HashMap<>();
        map.put("counselorList", counselorList);
        map.put("plannerList", plannerList);
        return ResultDataGenerator.genSuccessResult(map,"查询成功");
    }
    @ApiOperation("条件查询")
    @PostMapping("/byCondition")
    public ResultData<List<Counselor>> byCondition(@RequestBody CounselorQueryConditionReqDto counselorQueryCondition){
        List<Counselor> counselors= counselorService.byCondition(counselorQueryCondition);
        System.out.println("是否为空"+counselors);
        if(counselors.size()==0){
            return ResultDataGenerator.genSuccessResult(null,"没有数据");
        }
        return ResultDataGenerator.genSuccessResult(counselors,"查询成功");
    }
    @ApiOperation("查询全部擅长方向")
    @PostMapping("/adeptdirectionList")
    public ResultData<List<AdeptDirection>>adeptdirectionList(){
        Wrapper wrapper = new EntityWrapper<AdeptDirection>();
        wrapper.eq("is_active",1);
        List<AdeptDirection> list = adeptDirectionService.selectList(wrapper);
        if(list.size()==0){
            return ResultDataGenerator.genSuccessResult(null,"没有数据");
        }
        return ResultDataGenerator.genSuccessResult(list,"查询成功");
    }

}
